package com.lepu.app.fun.grow.play;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.view.WindowManager;

import com.app.application.MyApplication;
import com.app.utils.Const;
import com.app.utils.SdLocal;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.bean.PhotoItem;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class PlayImageUserActivity extends BaseFragmentActivity implements
		OnTopbarNewRightButtonListener {

	private ViewPager mViewPager;
	private static PlayImageUserActivity mInstance;

	public boolean mIsSample = true;
	public boolean mIsPlayAll = true;// 播放所有，从头开始播放
	public String mCurrentDateString = "";
	public boolean mIsPlayNet = false;// 播放网络下载的
	public String mAlbumID;
	public String[] mNetTitleArray;

	@Override
	@SuppressLint({ "InflateParams", "ResourceAsColor" })
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grow_play_image_user_activity);

		mInstance = this;
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInstance = null;
	}

	public static PlayImageUserActivity getInstance() {
		return mInstance;
	}

	private void init() {

		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.size() > 0) {
			mIsSample = bundle.getBoolean("boolean_sample");
			mIsPlayAll = bundle.getBoolean("boolean_all");
			mCurrentDateString = bundle.getString("current_day");
			mIsPlayNet = bundle.getBoolean("IsPlayNet");
			mAlbumID = bundle.getString("AlbumId");
			mNetTitleArray = bundle.getStringArray("titleArray");
		}

		ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		if (mIsPlayNet) {

			String folderPath = SdLocal.getAlbumHotPhotoFolder(this, mAlbumID);
			File file = new File(folderPath);
			File files[] = file.listFiles();
			
			for (int i = 0; i < files.length; i++) {
				PlayUserFragment fragment = new PlayUserFragment();
				Bundle data = new Bundle();
				//data.putString("title", "");
				data.putString("imagePath", files[i].getAbsolutePath());
				data.putInt("index", i);
				data.putString("bottom", mNetTitleArray[i]);
				fragment.setArguments(data);

				fragmentList.add(fragment);
			}
			
		} else if (mIsSample) {
			String[] sampleTopTitleArray = getResources().getStringArray(
					R.array.array_play_sample_top_title);
			String[] sampleBottomTitleArray = getResources().getStringArray(
					R.array.array_play_sample_bottom_title);

			for (int i = 0; i < Const.SAMPLE_PLAY_IMAGE_COUNT; i++) {
				PlayUserFragment fragment = new PlayUserFragment();
				Bundle data = new Bundle();
				data.putString("title", sampleTopTitleArray[i]);
				data.putString("imagePath", "");
				data.putInt("index", i);
				data.putString("bottom", sampleBottomTitleArray[i]);
				fragment.setArguments(data);

				fragmentList.add(fragment);
			}
		} else {
			ArrayList<PhotoItem> dataList = null;
			
			String uid = MyApplication.getInstance().getLoginInfo().UserID + "";
			if (mIsPlayAll) {
				dataList = PhotoItem.getAllPhotos(uid);
			} else {
				dataList = PhotoItem.getPhotosByDate(uid, mCurrentDateString);
			}

			for (int i = 0; i < dataList.size(); i++) {
				PhotoItem item = dataList.get(i);

//				int[] array = Utils.getCacuteTimeDayString(MyApplication
//						.getInstance().getLoginInfo().BabyBirthDate,
//						item.addTime);
//
//				String valueString = String
//						.format("%d岁%d天", array[0], array[1]);

				PlayUserFragment fragment = new PlayUserFragment();
				Bundle data = new Bundle();
				data.putString("title", "");//no use
				data.putString("imagePath", item.imagePath);// 播放自己的相册需要图片路径
				data.putInt("index", i);
				data.putString("bottom", item.desc);
				fragment.setArguments(data);

				fragmentList.add(fragment);
			}
		}

		mViewPager = (ViewPager) findViewById(R.id.viewPager);
		mViewPager.setOffscreenPageLimit(3);
		mViewPager.setAdapter(new FragmentPageAdapter(
				getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
	}

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mViewPager.setCurrentItem(index);
		}
	}

	@Override
	public void onTopbarRightButtonSelected() {
		finish(true);
	}

	@Override
	public void onBackPressed() {
		finish(true);
	}
}
